package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ConstantType;
import com.ruoyi.system.service.IConstantTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 常数类型Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "常数类型模块")
@RestController
@RequestMapping("/system/ConstantType")
public class ConstantTypeController extends BaseController {
    @Autowired
    private IConstantTypeService constantTypeService;

    /**
     * 查询常数类型列表
     */
    @ApiOperation(value = "查询常数类型列表")
    @PreAuthorize("@ss.hasPermi('system:ConstantType:list')")
    @GetMapping("/list")
    public Page<ConstantType> list(ConstantType entity, Page<ConstantType> page) {

        LambdaQueryWrapper<ConstantType> lambdaQueryWrapper = new LambdaQueryWrapper<ConstantType>();
        return constantTypeService.page(page, lambdaQueryWrapper);
    }

    /**
     * 导出常数类型列表
     */
    @ApiOperation(value = "导出常数类型列表")
    @PreAuthorize("@ss.hasPermi('system:ConstantType:export')")
    @Log(title = "常数类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConstantType constantType) {
        LambdaQueryWrapper<ConstantType> queryWrapper = new LambdaQueryWrapper<>();
        List<ConstantType> list = constantTypeService.list(queryWrapper);
        ExcelUtil<ConstantType> util = new ExcelUtil<ConstantType>(ConstantType.class);
        util.exportExcel(response, list, "常数类型数据");
    }


    /**
     * 获取常数类型详细信息
     */
    @ApiOperation(value = "获取常数类型详细信息")
    @PreAuthorize("@ss.hasPermi('system:ConstantType:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(constantTypeService.getById(id));
    }

    /**
     * 新增常数类型
     */
    @ApiOperation(value = "新增常数类型")
    @PreAuthorize("@ss.hasPermi('system:ConstantType:add')")
    @Log(title = "常数类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConstantType constantType) {
        return toAjax(constantTypeService.save(constantType));
    }

    /**
     * 修改常数类型
     */
    @ApiOperation(value = "修改常数类型")
    @PreAuthorize("@ss.hasPermi('system:ConstantType:edit')")
    @Log(title = "常数类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConstantType constantType) {
        return toAjax(constantTypeService.updateById(constantType));
    }


    /**
     * 删除常数类型
     */
    @ApiOperation(value = "删除常数类型")
    @PreAuthorize("@ss.hasPermi('system:ConstantType:remove')")
    @Log(title = "常数类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(constantTypeService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
